package com.geralt.im.parse.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.geralt.im.command.Command;
import com.geralt.im.constant.Constant;
import com.geralt.im.parse.IParser;
import com.geralt.im.protocol.Request;

public class RqParser implements IParser{
	
	
	private static RqParser rqParser=null;
	private RqParser(){
		
	}
	
	public static RqParser getInstance(){
		if(null==rqParser){
			rqParser = new RqParser();
		}
		return rqParser;
	}
	
	public static final Map<String, Command> keyCommandMap = new HashMap<String, Command>();
	static{
		try{
			Properties properties = new Properties();
			properties.load(new FileInputStream(new File("./conf/RequestType.properties")));
			Set<Object> set = properties.keySet();
			for(Object k:set){
				String commandName = Constant.COMMAND_PACK_NAME+(String)properties.get(k);
				Command command = (Command) Class.forName(commandName).getConstructor().newInstance();
				keyCommandMap.put((String)k, command);
			}
		
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public Command parse(Request request) {
		Command newCommand = keyCommandMap.get(request.getType());
		System.out.println(newCommand);
		newCommand.setParameter(request.getParameter());
		return newCommand;
	}
	
}
